package com.shuqing.download.engine.convert;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.StrUtil;
import com.shuqing.download.engine.config.EngineConfig;
import org.springframework.stereotype.Component;

import java.util.Date;

/**
 *  数据转换类
 * @author wangshuqing
 * @date 2023/2/27 18:42
 */
@Component
public class ConvertHelper {


    /**
    * 数据格式化
    * @date 2023/2/27 18:53
    * @author wangshuqing
    * @param value
    * @param format
     *@param defaultFormat
     *@return java.lang.Object
    * */
    public Object mapping(EngineConfig.Format defaultFormat,Object value, String format){
        if(value==null){
            return null;
        }
        if(value instanceof String){
            return value;
        }
        if(value instanceof Date){
            return dateMapping((Date) value, StrUtil.blankToDefault(format,defaultFormat.getDateFormat()));
        }
        if(value instanceof Number){
            return String.valueOf(value);
        }
        return value;
    }

    private String dateMapping(Date value,String format){
        return  DateUtil.format(value,format);
    }

}
